import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;
import com.speljohan.rsbot.script.wrappers.RSTilePath;

public class PowerCrayfish extends Script implements PaintListener {
	int levels;
	int startLevel;
	public long currentTime = System.currentTimeMillis();
	int cage = 13431;
	int spot = 6267;

	RSTile[] GetTheCagePath = new RSTile[] { new RSTile(3257, 3208),
			new RSTile(3249, 3206), new RSTile(3241, 3195),
			new RSTile(3243, 3186), new RSTile(3242, 3178),
			new RSTile(3243, 3167), new RSTile(3243, 3157) };
	RSTilePath GetCage = new RSTilePath(randomizePath(GetTheCagePath, 1, 1),
			this);

	RSTile[] GoBackPath = reversePath(GetTheCagePath);
	RSTilePath GoBack = new RSTilePath(randomizePath(GoBackPath, 1, 1), this);

	RSTile[] DeathPath = new RSTile[] { new RSTile(3221, 3218),
			new RSTile(3231, 3219), new RSTile(3241, 3216),
			new RSTile(3251, 3212), new RSTile(3256, 3207),
			new RSTile(3258, 3204) };
	RSTilePath Death = new RSTilePath(randomizePath(DeathPath, 1, 1), this);

	public boolean atPlace() {
		if (distanceTo(new RSTile(3256, 3205)) <= 10) {
			return true;
		} else if (distanceTo(new RSTile(3244, 3157)) <= 10) {
			return true;
		} else {
			return false;
		}
	}

	public void DeathWalk() {
		try {
			Death.walkToEnd();
		} catch (final Exception e) {
			log("Error deathwalking");
			stopAllScripts();
		}
	}

	public void Fish() {
		final RSNPC FishSpot = getNearestNPCByID(spot);
		atNPC(FishSpot, "Cage");
		wait(random(1000, 2000));
	}

	@Override
	public String getAuthor() {
		return "Jimmy5410";
	}

	public void getCage() {
		if (getEnergy() > 25) {
			setRun(true);
		}
		try {
			GetCage.walkToEnd();
		} catch (final Exception e) {
			log("Error walking, logging out...");
			stopAllScripts();
		}
		final RSNPC tutor = getNearestNPCByName("Fishing Tutor");
		atNPC(tutor, "Talk-to");
		wait(random(500, 1000));
		if (RSInterface.getInterface(234).getText().contains("Can you")) {
			clickMouse(260, 390, 3, 3, true);
			wait(random(500, 1000));
		}
		while (canContinue()) {
			clickContinue();
			wait(random(500, 1000));
		}
		if (RSInterface.getInterface(230).getText().contains("Crayfish")) {
			clickMouse(260, 390, 3, 3, true);
			wait(random(500, 1000));
		}
		while (canContinue()) {
			clickContinue();
			wait(random(500, 1000));
		}
		if (getInventoryCount(cage) > 0) {
			try {
				GoBack.walkToEnd();
			} catch (final Exception e) {
				log("Error getting cage, logging out...");
				stopAllScripts();
			}
		}
	}

	@Override
	public String getName() {
		return "Crayfish Powerfisher";
	}

	@Override
	public String getScriptCategory() {
		return "Fishing";
	}

	@Override
	public String getScriptDescription() {
		String html = "<html>";
		html += "<body><center>";
		html += "<h2>" + getName() + "</h2></center>";
		html += "<b>Author: </b>" + getAuthor() + "<br>";
		html += "<b>Version: </b>" + getVersion() + "<br>";
		html += "<center>Start anywhere.<br></center>";
		html += "</body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() {
		return 1.00;
	}

	public void HomeTele() {
		if (getCurrentTab() != Constants.TAB_MAGIC) {
			openTab(Constants.TAB_MAGIC);
		}
		clickMouse(570, 230, 3, 3, true);
		wait(random(10000, 15000));
	}

	@Override
	public int loop() {
		if (atPlace()) {
			if (isInventoryFull()) {
				dropAllExcept(cage);
			}

			else if (!isInventoryFull()) {
				if (inventoryContains(cage)) {
					if (getMyPlayer().getAnimation() != -1) {
						wait(random(3000, 6000));
					} else {
						Fish();
					}
				}
				if (!inventoryContains(cage)) {
					getCage();
				}
			}
		}
		if (!atPlace()) {
			HomeTele();
			DeathWalk();
		}

		return 0;
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (startLevel == 0) {
			startLevel = skills.getCurrentSkillLevel(Constants.STAT_FISHING);
		}
		levels = skills.getCurrentSkillLevel(Constants.STAT_FISHING)
				- startLevel;

		long millis = System.currentTimeMillis() - currentTime;
		final long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		final long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		final long seconds = millis / 1000;
		g.setColor(Color.GREEN);
		g.drawString("Crayfish Powerfisher v" + getVersion(), 25, 265);
		g.drawString("Runtime: " + hours + "hour(s) " + minutes + " minute(s) "
				+ seconds + " second(s)", 25, 280);
		g.drawString("Levels Gained: " + levels, 25, 295);

	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		return true;
	}
}